<template>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">表单编辑器</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <!-- 有二级菜单的按钮 -->
                    <template v-for="menu in menus">
                        <li class="dropdown" v-if="menu.type === 'dropdown'">
                            <a href="#"
                               class="dropdown-toggle"
                               :class="menu.extraCls"
                               :title="menu.title"
                               data-toggle="dropdown"
                               role="button"
                               aria-haspopup="true"
                               aria-expanded="false"><i :class="menu.icon"
                                   aria-hidden="true"
                                   v-if="menu.icon"></i>{{ menu.name }}
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu">
                                <li v-for="subMenu in menu.subMenus">
                                    <template v-if="subMenu.type === 'default'">
                                        <a href="#"
                                           :class="subMenu.extraCls"
                                           :title="subMenu.title"
                                           @click.prevent="clickCallback(subMenu.callback)">
                                            <i :class="subMenu.icon"
                                               aria-hidden="true"
                                               v-if="subMenu.icon"></i>{{ subMenu.name }}</a>
                                    </template>
                                </li>
                            </ul>
                        </li>
                        <!-- 普通按钮 -->
                        <li v-else>
                            <a href="#"
                               :class="menu.extraCls"
                               :title="menu.title"
                               @click="clickCallback(menu.callback)">
                                <i :class="menu.icon"
                                   aria-hidden="true"
                                   v-if="menu.icon"></i>{{ menu.name }}
                            </a>
                        </li>
                    </template>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>

</template>
<script>
import { mapGetters } from 'vuex'
export default {
    name: 'Navbar',
    computed: {
        ...mapGetters({
            menus: 'navbarMenus'
        })
    },
    methods: {
        clickCallback (callback) {
            this.$store.dispatch(callback[0], callback[1])
        }
    }
}
</script>
